package com.xbongbong.pro.statistics.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.enums.ReturnTypeEnum;
import com.xbongbong.pro.statistic.enums.SystemCodeEnum;
import com.xbongbong.pro.statistic.result.pojo.ChartResultPojo;
import com.xbongbong.pro.statistic.result.pojo.SalesReportPojo;
import com.xbongbong.pro.statistic.result.pojo.vo.SalesReportVO;
import com.xbongbong.pro.statistic.service.ChartResultService;
import com.xbongbong.pro.statistic.service.ResultThroughService;
import com.xbongbong.pro.statistic.service.result.ChartResult;
import com.xbongbong.pro.statistic.through.pojo.dto.ResultThroughDTO;
import com.xbongbong.pro.statistic.through.pojo.vo.ResultThroughVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author zcp
 * @version v1.0
 * @date 2019/4/15 16:00
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/chart")
public class ChartResultController {
    private static final Logger LOG = LoggerFactory.getLogger(ChartResultController.class);
    @Resource
    private ChartResultService chartResultService;
    @Resource
    private ResultThroughService resultThroughService;
    @Resource
    private ChartResult chartResult;

    /**
     * 统计销售简报结果，并入库tb_target_log
     * @param salesReportPojo 入参
     * @param br 校验信息
     * @return java.lang.String
     * @throws XbbException
     * @author zcp
     * @date 2019/4/15 16:18
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/result/feign/salesReport", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String feignSalesReport(@RequestBody @Valid SalesReportPojo salesReportPojo, BindingResult br) {
        XbbResponse<SalesReportVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SalesReportVO salesReportVo = chartResultService.chartResultSalesReport(salesReportPojo);
                response = new XbbResponse<>(salesReportVo);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 得到指标总额的统计结果
     * 注：该方法得到的结果为String类型的Object，其中double的格式为“##,###.##”，如果外部接收到后需要转为数字，则应该处理逗号的问题
     * @param chartResultPojo 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zcp
     * @date 2019/5/23 10:10
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/result/feign/value", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String feignValue(@RequestBody @Valid ChartResultPojo chartResultPojo, BindingResult br) {
        XbbResponse<Object> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SystemCodeEnum systemCodeEnum = chartResultPojo.getSystemCodeEnum();
                //得到总额
                Object object = chartResult.getResult4System(ReturnTypeEnum.VALUE_RETURN, systemCodeEnum.getXbbRefTypeEnum(), chartResultPojo, null);
                response = new XbbResponse<>(object);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * web端统计穿透
     * @param resultThroughDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zcp
     * @date 2019/4/26 14:17
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/result/through", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartResultThrough(@RequestBody @Valid ResultThroughDTO resultThroughDTO, BindingResult br) {
        XbbResponse<ResultThroughVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ResultThroughVO resultThroughVo = resultThroughService.chartResultThrough(resultThroughDTO);
                response = new XbbResponse<>(resultThroughVo);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * app端统计穿透
     * @param resultThroughDTO 入参
     * @param br 校验信息
     * @return java.lang.String
     * @author zcp
     * @date 2019/5/11 20:41
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/mobile/result/through", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartMobileResultThrough(@RequestBody @Valid ResultThroughDTO resultThroughDTO, BindingResult br) {
        XbbResponse<ResultThroughVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ResultThroughVO resultThroughVo = resultThroughService.chartMobileResultThrough(resultThroughDTO);
                response = new XbbResponse<>(resultThroughVo);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
